module afullfig
import StdEnv, japi

/* This program shows how to draw a filled polygonal.
   Click at different points of the canvas, and after
   the third click, you wil get a filled polygonal.
   You can save the result of your work as ppm.
************************************************ */

                                      // ABREVIATIONS:
:: State = { fr::!Framer,             // fr::!F 
             canvas::!Canvas,         // canvas::!CP
             mousepressed::!Device,   // mousepressed::!D
             mousereleased::!Device,  // mousereleased::!D
             quit::!Entry,            // quit::!E
             save::!E,
             erase::!E,
             fillcolor::!(!Int, !Int, !Int),
             xs::![(!Int, !Int)]}            



Start world
  # (js, world) = begin_japi world
    (frm, js) = jnewFrame (WH 600 550) "Draw Things" js
    (cv, js) = jnewCanvas (XYWH 0 0 600 550) frm  js
    cv = bg_color yellow   cv
    cv= jsetfont (TimesNew 20) cv
    txt= "Click the mouse on the canvas many times!"
    cv= jdraw  (Txt (30, 500) txt) cv
    cv= jsetfont (TimesNew 12) cv
    (cv, js) = jcliprect cv 30 40 400 370 js
    cv = bg_color  white cv
    (mnb, js) = jnewBar frm js
    (filemn, js) = jnewMenu  "File" mnb js 
    (sv, js) = jnewItem "Save" filemn  js
    (qt, js) = jnewItem  "Quit" filemn js
    (editmn, js) = jnewMenu  "Edit" mnb js 
    (er, js) = jnewItem "Erase" editmn  js
    (mp, cv) = jmousepressed cv
    (mr, cv) = jmousereleased cv
    (frm, js) = jshow frm js
    s= { fr= frm, canvas= cv, xs= [],
         mousepressed= mp, mousereleased= mr,
         fillcolor= red,
         quit= qt, save= sv, erase= er}
    js = loop s js
  = end_japi js world
where
	loop :: !State !*JS -> *JS
	loop s js
		# 	(na, js)= jnextaction js
		|	na== (act s.fr) || na== (act s.quit)
		       # js= jdispose s.canvas js 
		       = jquit js
		| na== (act s.save) 
             # img= jgetimage s.canvas
             # (fname, js) = jfilesel s.fr "*.ppm" js
             # (_, js) = jsaveppm img fname js 
             = loop s js
        | na== (act s.erase)
             # cv= bg_color white s.canvas
             # (r, js)= jrandom 255 js
             # (g, js)= jrandom 255 js
             # (b, js)= jrandom 255 js
             # fc= (r, g, b)
             # news= {s & canvas= cv, xs=[], fillcolor= fc} 
             = loop  news js
        |   na== (act s.mousereleased)
                = loop (card s) js
		|	na== (act s.mousepressed)
		        # m= s.mousepressed
		        # p= mxy m
				= loop {s& xs= [p:s.xs]} js
		= loop s js
	      
	card   s 
		# 	cv=  fill [ Hue s.fillcolor,
                        Plg s.xs,
                        Hue black] s.canvas
			cv=  jdraw (Plg s.xs) cv
      	= {s& canvas= cv}

		
		 